<script type="text/javascript">
	function CourseManagerCtrl($scope, $http, modalService, CtrlUtil, $element, UploadService) {
		var ctrl = this
		CtrlUtil.build(ctrl, ':root/course/queryCourse')
		this.query = function () {
			ctrl.entities && (ctrl.entities.content = {})
			var p = CtrlUtil.getQueryParams(ctrl)
			$http.post(':root/course/time').success(function (r) {
				ctrl.time = r
				return r
			}).success(function () {
				$http.post(':root/course/queryCourse', p).success(function (r) {
					ctrl.entities = r;
					var ids = []
					angular.forEach(r.content, function (v) {
						ids.push(v.id)
						var status = v.status
						if (status == 1 || ctrl.time >= v.kcsj) {
							v.statusString = '已过期'
							//v.status = 1
						} else {
							v.statusString = '即将开课'
							//v.status = 0
						}
					})
					$http.post(':root/course/getCourseApplyCount', {
						ids: ids
					}).success(function (R) {
						var map = {}
						angular.forEach(R, function (v) {
							map[v[0]] = v[1]
						})
						angular.forEach(r.content, function (v) {
							v.applyCount = map[(v.id)]
						})
					})
				})
			})
		}
		$http.post(':root/term/queryTerms').success(function (r) {
			ctrl.terms = r.content
			ctrl.termQueryParam = ctrl.terms.length > 0 ? ctrl.terms[0].id : ''
		})
		this.edit = function (item) {
			ctrl.add(item)
		}
		this.preview = function (item) {
			var title = '课程预览' || $element.find('[template]>[edit-title]').clone()
			var result = item
			if (item) {
				result.imageUrl = CtrlUtil.toImageUrl(item.tpId, 1)
			}
			modalService.show({
				title: title,
				contentUrl: ':root/components/common/courseView.html',
				size: 'lg',
				titleCls: '',
				result: result
			})
		}
		this.showApplyStudents = function (item) {
			if (item.applyCount) {
				modalService.show({
					title: '已报名学生详情',
					contentUrl: ':root/components/manager/courseStuInfo.html',
					size: 'lg',
					courseId: item.id,
					course: item,
					supCtrl: ctrl
				})
			}
		}
		this.del = function (item) {
			modalService.confirm({
				content: '确定要删除课程' + item.name + '吗?',
				result: item
			}).then(function (r) {
				$http.post(':root/course/deleteCourse', r).success(function (R) {
					modalService.succ('删除成功', 1500)
					ctrl.query()
				})
			})

		}
		this.add = function (item) {
			var content = $element.find('[template]>[edit]').clone()
			var title = $element.find('[template]>[edit-title]').clone()
			var btn = content.find('[file]')
			var result = item
			result = result || {
				imageUrl: 'asset/img/test.jpg',
				uploadPercent: 0,
				fbsj: new Date(),
				fbr: '系统管理员'
			}
			if (item) {
				result.imageUrl = CtrlUtil.toImageUrl(item.tpId, 1)
			}
			var config = {
				multipleFiles: false,
				autoUploading: true,
				browser: btn[0],
				maxSize: 1024 * 1024 * 20,
				extFilters: [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
				onComplete: function (file) {
					var attach = {}
					attach.fileName = file.name;
					attach.hash = eval('(' + file.msg + ')').message;
					attach.size = file.size;
					attach.uploading = false
					result.tpId = (attach.hash + '/' + attach.size)
					result.imageUrl = CtrlUtil.toImageUrl(result.tpId, 1)
					$scope.$$phase || $scope.$apply()
				},
				onSelect: function (list) {
					var notImage = false, overSize = false
					angular.forEach(list, function (v) {
						if (v.size > (1024 * 1024 * 20)) {
							overSize = true
						}
					})
					if (overSize) {
						toaster.pop({
							type: 'error',
							body: '文件大小不能超过20M',
							timeout: 800
						});
						$scope.$$phase || $scope.$apply()
					}
				}
			}
			var stream = UploadService.create(config)
			modalService.confirm({
				title: title,
				content: content,
				size: 'lg',
				titleCls: '',
				result: result,
				$scope: $scope,
				validator: function (r) {

				}
			}, {
					backdrop: false
				}).then(function (r) {
					$http.post(':root/course/addCourse', r).success(function (R) {
						modalService.succ(item ? '修改成功' : '添加成功', 1500)
						ctrl.query()
					})
				})
		}
	}

</script>
<script type="text/javascript">
	function CourseApplyStuInfoCtrl($scope, CtrlUtil, $http) {
		var ctrl = this;
		this.courseIdQueryParam = $scope.courseId
		CtrlUtil.loadDepartment(ctrl)
		this.getClassInfo = function (term, target) {
			target = target || ctrl
			return $http.post(':root/trainee/getClassInfoList', {
				term: term
			}).success(function (r) {
				target.classInfoList = r
			})
		}
		this.getClassInfo($scope.course.term.id)
		CtrlUtil.build(this, ':root/course/queryStuInfoByCourseId')
	}

</script>
<div ng-controller="CourseManagerCtrl as ctrl" ng-init="ctrl.query()">
	<!-- Search area -->
	<div class="panel panel-cys">
		<div class="panel-heading">
			<h3 class="panel-title">
				<span class="glyphicon glyphicon-search"></span> 信息查询
			</h3>
		</div>
		<div class="panel-body">
			<div class="container-fluid">
				<form class="form-horizontal">
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select class="form-control" ng-change="ctrl.getClassInfo(m.id)" chosen ng-options="m.id as (m.name+'-第'+m.qs+'期') for m in ctrl.terms" ng-model="ctrl.termQueryParam">
								<option value="">期次</option>
							</select>
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="课程名称" ng-model="ctrl.nameQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<select chosen ng-model="ctrl.typeQueryParam" class="form-control">
								<option value="">课程类别</option>
								<option value="1">必修课</option>
								<option value="2">选修课</option>
								<option value="3">体验课</option>
							</select>
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="主讲人" ng-model="ctrl.zjrQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<input placeholder="主办单位" ng-model="ctrl.zbdwQueryParam" type="text" class="form-control">
						</div>
					</div>
					<div class="form-group col-md-4 remove-margin-horizontal">
						<div class="col-md-10 col-md-offset-1">
							<div class="form-control-static">&nbsp;</div>
						</div>
					</div>
				</form>
				<div class="row pull-right">
					<div class="col-md-12">
						<button ng-click="ctrl.query()" type="submit" class="btn btn-cys btn-sm">
							<span class="glyphicon glyphicon-search"></span>
							查询
						</button>
						<button ng-click="ctrl.reset()" type="reset" class="btn btn-default btn-sm">
							<span class="glyphicon glyphicon-repeat"></span>
							重置
						</button>
					</div>
				</div>
			</div>
		</div>

	</div>
	<!-- Search area END -->

	<!-- operation area -->
	<a type="button" class="btn btn-cys btn-sm" ng-click="ctrl.add()">
		<span class="glyphicon glyphicon-plus"></span>
		新增
	</a>
	<a type="button" class="btn btn-cys btn-sm" ng-click="export(ctrl,'root/course/exportCourse')">
		<span class="glyphicon glyphicon-export"></span>
		导出
	</a>
	<div class="pull-right" ng-include="':root/components/page.html'"></div>
	<!-- operation area END -->
	<!-- data grid area -->
	<div style="padding-top: 10px;">
		<table class="table table-bordered table-hover table-striped">
			<tr>
				<th>
					<input ng-change="ctrl.checkAll(ctrl.$checkAll)" ng-model="ctrl.$checkAll" type="checkbox" />
				</th>
				<th>课程名称</th>
				<th>课程类型</th>
				<th>主讲人</th>
				<th>开课时间</th>
				<th>开课状态</th>
				<th>发布时间</th>
				<th>课程名额</th>
				<th>已报名人数</th>
				<th>操作</th>
			</tr>
			<tr ng-repeat="item in ctrl.entities.content">
				<td><input ng-model="item.$checked" type="checkbox" /></td>
				<td>{{item.name}}</td>
				<td>{{item.kclb|courseType}}</td>
				<td>{{item.zjr}}</td>
				<td>{{item.kcsj|date:'yyyy-MM-dd HH:mm'}}</td>
				<td>{{item.statusString}}</td>
				<td>{{item.fbsj|date:'yyyy-MM-dd HH:mm'}}</td>
				<td>{{item.kcme}}</td>
				<td><a href="javascript:;" ng-click="ctrl.showApplyStudents(item)">{{item.applyCount||0}}</a></td>
				<td><a href="javascript:;" ng-click="ctrl.edit(item)">编辑</a> <a href="javascript:;" ng-click="ctrl.preview(item)">预览</a> <a href="javascript:;"
					 ng-click="ctrl.del(item)">删除</a></td>
			</tr>
		</table>
		<p>
			共有记录：
			<span>{{ctrl.entities.totalElements}}</span> 条
		</p>
	</div>
	<!-- data grid area END -->
	<div ng-non-bindable template class="hidden">
		<h3 class="panel-title" edit-title>
			<span class="glyphicon glyphicon-search"></span> 课程发布
		</h3>
		<div edit>
			<div class="panel-body">
				<form class="form-horizontal" ng-if="result">
					<div class="row">
						<div class="col-md-8">
							<div class="form-group">
								<div class="col-md-12">
									<input ng-model="result.name" name="result.name" placeholder="课程名称" title="请输入课程名称" type="text" class="form-control">
								</div>
							</div>
							<div class="form-group">
								<div class="col-md-6">
									<input ng-model="result.zbdw" name="result.zbdw" type="text" placeholder="主办单位" title="请输入主办单位" class="form-control">
								</div>

								<div class="col-md-6">
									<input ng-model="result.zjr" name="result.zjr" placeholder="主讲人" title="请输入主讲人" type="text" class="form-control">
								</div>
							</div>
							<div class="form-group">
								<div class="col-md-12">
									<input ng-model="result.kkdd" name="result.kkdd" type="text" placeholder="开课地点" title="请输入开课地点" class="form-control">
								</div>
							</div>
							<div class="form-group">
								<div class="col-md-6">
									<input ng-model="result.kcsj" name="result.kcsj" datetimepicker placeholder="开课时间" title="请输入开课时间" type="text" class="form-control">
								</div>
								<div class="col-md-6">
									<data entity="Term" auto-load="true" after-refresh="terms=data">
										<filter field="status" value="1"></filter>
									</data>
									<select class="form-control" ng-model="result.term.id" ng-options="t.id as (t.trainType.name+t.name) for t in terms">
										<option value="">请选择期次</option>
									</select>
								</div>
								<!--  
								<div class="col-md-6">
									<input ng-model="result.kksc" name="result.kksc" placeholder="开课时长" title="请输入开课时长" type="text" class="form-control">
								</div>
								-->
							</div>
						</div>
						<div class="col-md-4">
							<div class="">
								<div class="btn thumbnail" file title="点击更换图片,建议尺寸280x170">
									<img class="img-responsive" style="max-height: 160px;" ng-src="{{result.imageUrl}}" alt="...">
									<div class="text-muted" style="position: absolute; width: 100%; text-align: center; bottom: 2px;">建议图片尺寸280x170</div>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-md-8">
							<div class="form-group">
								<div class="col-md-6">
									<select chosen ng-model="result.kclb" class="form-control" name="result.kclb">
										<option value="">课程类别</option>
										<option value="1">必修课</option>
										<option value="2">选修课</option>
										<option value="3">体验课</option>
									</select>
								</div>
								<div class="col-md-6">
									<input ng-model="result.kcme" name="result.kcme" placeholder="课程名额" title="请输入课程名额" type="number" class="form-control">
								</div>
							</div>
						</div>
						<div class="col-md-4">
							<div class="form-group">
								<div class="col-md-12">
									<input ng-model="result.qdsj" name="result.qdsj" placeholder="签到时间" title="请输入签到时间" type="text" class="form-control">
								</div>
							</div>
						</div>
					</div>
					<!--
					<div class="row">
						<div class="col-md-8">
							<div class="form-group">
								<div class="col-md-6">
									<input ng-model="result.fzr" name="result.fzr" placeholder="负责人" title="请输入负责人" type="text" class="form-control">
								</div>
								<div class="col-md-6">
									<input ng-model="result.fzrdh" name="result.fzrdh" placeholder="负责人电话" title="请输入负责人电话" type="text" class="form-control">
								</div>
							</div>
						</div>
						<div class="col-md-4">
							<div class="form-group">
								<div class="col-md-12">
									<input ng-model="result.fzremail" name="result.fzremail" placeholder="负责人邮箱" title="请输入负责人邮箱" type="text" class="form-control">
								</div>
							</div>
						</div>
					</div>
					-->
					<div class="form-group">
						<div class="col-md-12">
							<label for="exampleInputEmail1">课程简介</label>
							<textarea ng-model="result.kcjj" name="result.kcjj" class="form-control" style="resize: none;" rows="3"></textarea>
						</div>

					</div>
					<div class="form-group">
						<div class="col-md-12">
							<label for="exampleInputEmail1">主讲人简介</label>
							<textarea ng-model="result.zjrjj" name="result.zjrjj" class="form-control" style="resize: none;" rows="3"></textarea>
						</div>
					</div>
					<div class="form-group">
						<div class="col-md-12">
							<label for="exampleInputEmail1">学习备注</label>
							<textarea ng-model="result.xxbz" name="result.xxbz" class="form-control" style="resize: none;" rows="3"></textarea>
						</div>
					</div>
					<div class="row">
						<div class="col-md-8">
							<div class="form-group">
								<div class="col-md-6">
									<input ng-model="result.fbr" name="result.fbr" placeholder="发布人" title="发布人" type="text" class="form-control">
								</div>
								<div class="col-md-6">
									<input ng-model="result.fbsj" name="result.fbsj" placeholder="发布时间" datetimepicker title="发布时间" type="text" class="form-control">
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
</div>